<?
/**
 * Pages for Core
 * Version 2.0
 * Приклад використання змiнних в Smarty:
 * {if count($pages_list)}
 *	Сторінка: 
 *	{if $prev_page}
 *	  <a href="?{$query_string}&page={$prev_page}">&lt;&lt;</a>
 *	{/if}
 *	{foreach item=p from=$pages_list}
 *	{if $p == $smarty.session.pages.page}
 *		<b>{$p}</b>
 *	{else}
 *		<a href="?{$query_string}&page={$p}">{$p}</a>
 *	{/if}
 *	{/foreach}
 *	{if $next_page}
 *	  <a href="?{$query_string}&page={$next_page}">&gt;&gt;</a>
 *	{/if}
 *{/if}
 *
 *
 */

class Pages
{
	public $itemsShowPages=10;
	public $itemsPerPage=10;
	public $page = 1;
	public $checkSession = true;
	public $dialog = null;
	
	function __construct(&$dialog,$pageListSufix='',$checkSession = true) {
		global $options;
		$this->pageListSufix = $pageListSufix;
		$this->checkSession = $checkSession;
		$this->dialog = $dialog;
		
		
		
		# Check CONFIG file
		if (isset($options['pages'.$this->pageListSufix]['itemsShowPages'])) {
			$this->itemsShowPages = $options['pages'.$this->pageListSufix]['itemsShowPages'];
		}
		if (isset($options['pages'.$this->pageListSufix]['itemsPerPage'])) {
			$this->itemsPerPage = $options['pages'.$this->pageListSufix]['itemsPerPage'];			
		}
		
		if ($this->checkSession) {
			# Check SESSION
			if (isset($_SESSION['pages'.$this->pageListSufix]['itemsShowPages'])) {
				$this->itemsShowPages = $_SESSION['pages'.$this->pageListSufix]['itemsShowPages'];
			}
			if (isset($_SESSION['pages'.$this->pageListSufix]['itemsPerPage'])) {
				$this->itemsPerPage = $_SESSION['pages'.$this->pageListSufix]['itemsPerPage'];				
			}
			
			if (isset($_SESSION['pages'.$this->pageListSufix]['page'])) {
				$this->page = $_SESSION['pages'.$this->pageListSufix]['page'];
			}
		}
		$page = 'page'.$this->pageListSufix;
		if ($dialog->$page > 0) {
			$this->page = $this->dialog->$page;
		}
		$this->start_item = ($this->page-1)*$this->itemsPerPage;
	}
	
	function generatePageList($count) {
		$this->count = $count;
		$this->pages_list = array();
		$pages = ceil($this->count/$this->itemsPerPage);
		$start_page = floor(($this->page-1)/$this->itemsShowPages)*$this->itemsShowPages;
		if ($start_page) {
		   $this->prev_page = $start_page;
		}
		$end_page = $pages;
		if (($start_page+$this->itemsShowPages)<$pages) {
		   $end_page = $start_page+$this->itemsShowPages;
		   $this->next_page = $end_page+1;
		}
		for($i = $start_page;$i<$end_page;$i++) {
			array_push($this->pages_list,($i+1));
		}
		#print_r($this);
		#print_r($_SESSION);
		#print $this->start_item;
		$this->setupDialog();
		
	}
	
	function setupDialog() {
		if ($this->checkSession) {
			# Setup SESSION
			if (!is_array($_SESSION['pages'.$this->pageListSufix])) {
				$_SESSION['pages'.$this->pageListSufix] = array();
			}
			$_SESSION['pages'.$this->pageListSufix]['page'] = $this->page;
		}
		
		$prev_page = 'prev_page'.$this->pageListSufix;
		$this->dialog->$prev_page = $this->prev_page;
		
		$next_page = 'next_page'.$this->pageListSufix;
		$this->dialog->$next_page = $this->next_page;
		
		$pages_list = 'pages_list'.$this->pageListSufix;
		$this->dialog->$pages_list = $this->pages_list;
		
		$count = 'count'.$this->pageListSufix;
		$this->dialog->$count = $this->count;
		
	}
	
}

?>